package com.yds.xutils_facrotypattern;

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.permissionx.guolindev.PermissionX;
import com.permissionx.guolindev.callback.ExplainReasonCallbackWithBeforeParam;
import com.permissionx.guolindev.callback.ForwardToSettingsCallback;
import com.permissionx.guolindev.callback.RequestCallback;
import com.permissionx.guolindev.request.ExplainScope;
import com.permissionx.guolindev.request.ForwardScope;
import com.yds.xutils_facrotypattern.https.ApiConstanse;
import com.yds.xutils_facrotypattern.https.HttpManager;
import com.yds.xutils_facrotypattern.https.HttpRequestCallBack;

import org.xutils.http.RequestParams;

import java.util.List;

@SuppressWarnings("all")
public class MainActivity extends AppCompatActivity {

    private TextView tvRequestMsg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tvRequestMsg = findViewById(R.id.tvRequestMsg);

        requestPermissions();

        RequestParams params = new RequestParams(ApiConstanse.BASE_URL + ApiConstanse.QUERY_DATA);
        params.addQueryStringParameter("name","yds");
        HttpManager.request().httpsPost(params, new HttpRequestCallBack() {
            @Override
            public void onSuccess(String result) {
                tvRequestMsg.setText(result);
            }

            @Override
            public void onFail(String errMsg) {
                tvRequestMsg.setText(errMsg);
            }
        });
    }

    /**
     * @auther 于德水
     * created at 2021/7/19 16:23
     * 方法描述：请求权限
     */
    private void requestPermissions() {
        PermissionX.init(this)
                .permissions(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .onExplainRequestReason(new ExplainReasonCallbackWithBeforeParam() {
                    @Override
                    public void onExplainReason(ExplainScope scope, List<String> deniedList, boolean beforeRequest) {
                        scope.showRequestReasonDialog(
                                deniedList,
                                "即将申请的权限是程序必须依赖的权限",
                                "我已明白"
                        );
                    }
                })
                .onForwardToSettings(new ForwardToSettingsCallback() {
                    @Override
                    public void onForwardToSettings(ForwardScope scope, List<String> deniedList) {
                        scope.showForwardToSettingsDialog(
                                deniedList,
                                "您需要去应用程序设置当中手动开启权限",
                                "我已明白"
                        );
                    }
                })
                .request(new RequestCallback() {
                    @Override
                    public void onResult(boolean allGranted, List<String> grantedList, List<String> deniedList) {
                        //所有申请的权限都已通过
                        if (allGranted) {

                        } else {
                            Toast.makeText(
                                    MainActivity.this,
                                    "您拒绝了如下权限：$deniedList",
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

}